
/** 范围随机数 */
export const random = (start: number, end: number) =>
  Math.floor(Math.random() * (end - start) + start)

/** 随机十六进制颜色 */
export const randomHexColor = () => {
  let result = '#'
  for (let i = 0; i < 3; i++) {
    result += random(0, 255).toString(16).padStart(2, '0')
  }

  return result
}

/** 计算两个坐标的距离 */
export const baseDistance = (
  x1: number,
  y1: number,
  x2: number,
  y2: number
) => {
  const distance = Math.sqrt(Math.abs(x1 - x2) ** 2 + Math.abs(y1 - y2) ** 2)
  return distance
}

/** 深拷贝对象 */
export const deepClone = <T>(
  tSource: T,
  tTarget?: Record<string, unknown> | T
): T => {
  tTarget = tTarget || (Array.isArray(tSource) ? [] : {}) as T
  for (const key in tSource) {
    if (Object.prototype.hasOwnProperty.call(tSource, key)) {
      if (typeof tSource[key] === 'object' && tSource[key] !== null) {

        // eslint-disable-next-line
        tTarget[key] = Array.isArray(tSource[key]) ? [] : {} as any
        deepClone(tSource[key], tTarget[key])
      } else {
        tTarget[key] = tSource[key]
      }
    }
  }

  return tTarget as T
}
